package me.shiqiang.im.action;

import com.tst.common.returnobj.BaseReturnObj;
import com.tst.common.returnobj.ReturnObj;
import com.tst.common.returnobj.ReturnObjCode;
import me.shiqiang.im.domain.ServerNode;
import me.shiqiang.im.redis.RedisKey;
import me.shiqiang.im.service.ServerNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author cuishiqiang
 * @date 2017/12/14 下午4:27
 */
@RestController
@RequestMapping("im/server")
public class ServerNodeAction {

    @Autowired
    ServerNodeService serverNodeService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PutMapping("register/{serverId}")
    public void register(@PathVariable String serverId, String hostInfo) {
        ServerNode serverNode = new ServerNode();
        serverNode.setServerId(serverId);
        serverNode.setAddress(hostInfo);
        serverNode.setTotal(10000L);
        serverNode.setUsed(0L);
        serverNodeService.save(serverNode);
    }

    @GetMapping("{userId}/serverAddress")
    public ReturnObj<String> getServerIp(@PathVariable String userId) {
        String serverIp = serverNodeService.getServerAddress(userId);
        return new ReturnObj<>(ReturnObjCode.SUCCESS, serverIp);
    }

    @GetMapping("{userId}/serverId")
    public String getServerId(@PathVariable String userId) {
        return serverNodeService.getServerId(userId);
    }

    @PutMapping("{userId}/serverAddress/{serverIp}")
    public BaseReturnObj setUserServerIp(@PathVariable String userId, @PathVariable String serverIp){
        serverNodeService.setUserServerIp(userId, serverIp);
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"设置成功");
    }


    @PostMapping("{userId}/allocateServer")
    public BaseReturnObj allocateServer(@PathVariable String userId) {
        serverNodeService.allocateServer(userId, new HashSet<>());
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"设置成功");
    }

    @GetMapping("{serverId}/{groupId}/needNotifyUsers")
    public Collection<String> get(@PathVariable String serverId, @PathVariable Long groupId){
        return serverNodeService.getNeedNotifyUsers(serverId,groupId);
    }

    @PutMapping("groupServer/{groupId}/add/{serverId}")
    public void addToGroup(@PathVariable String groupId,@PathVariable String serverId) {
        serverNodeService.addToGroup(RedisKey.groupServerSet(groupId), serverId);
    }

    @DeleteMapping("groupServer/{groupId}/add/{serverId}")
    public void deleteFromGroup(@PathVariable String groupId,@PathVariable String serverId) {
        serverNodeService.deleteFromGroup(RedisKey.groupServerSet(groupId), serverId);
    }

    @GetMapping("groupServer/{groupId}")
    public Set<String> getGroupServerIdSet(@PathVariable String groupId) {
        return serverNodeService.getGroupServerIdSet(RedisKey.groupServerSet(groupId));
    }
}
